package com.itheima.mall.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.mall.mapper.FlashPromotionMapper;
import com.itheima.mall.pojo.FlashPromotion;
import com.itheima.mall.service.SmsFlashPromotionService;
import com.itheima.mall.vo.PageResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;

@Service
public class SmsFlashPromotionServiceImpl implements SmsFlashPromotionService {

    @Autowired
    private FlashPromotionMapper flashPromotionMapper;

    /**
     * 添加活动
     */
    @Override
    public void create(FlashPromotion flashPromotion) {
        //设置属性
        flashPromotion.setCreateTime(new Date());
        flashPromotionMapper.insert(flashPromotion);
    }

    /**
     * 删除活动
     */
    @Override
    public void delete(Long id) {
        flashPromotionMapper.deleteById(id);
    }

    /**
     * 根据活动名称分页查询
     */
    @Override
    public PageResult list(String keyword, Integer pageNum, Integer pageSize) {
        Page<FlashPromotion> page = new Page<>(pageNum,pageSize);
        LambdaQueryWrapper<FlashPromotion> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(keyword != null,FlashPromotion::getTitle,keyword);
        Page<FlashPromotion> pageInfo = flashPromotionMapper.selectPage(page, queryWrapper);
        return new PageResult(pageNum,pageSize, (int) pageInfo.getTotal(),pageInfo.getRecords());
    }

    /**
     * 修改上下线状态
     */
    @Override
    public void updateStatus(Long id,Integer status) {
        LambdaUpdateWrapper<FlashPromotion> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper.eq(id != null,FlashPromotion::getId,id);
        updateWrapper.set(status != null,FlashPromotion::getStatus,status);
        flashPromotionMapper.update(null,updateWrapper);
    }

    /**
     * 编辑活动
     */
    @Override
    public void update(Long id, FlashPromotion flashPromotion) {
        flashPromotion.setId(id);
        flashPromotionMapper.updateById(flashPromotion);
    }

    /**
     * 获取活动详情
     */
    @Override
    public FlashPromotion flash(Long id) {
        return flashPromotionMapper.selectById(id);
    }

}
